﻿<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>

<%@ Register Src="../Shared/Header.ascx" TagName="Header" TagPrefix="uc1" %>
<!DOCTYPE html>
<html>
<head runat="server">
    <title>数据字典条目管理</title>
    <uc1:Header ID="Header1" runat="server" />
    <script type="text/javascript" language="javascript">

        var cid = "";

        $(function () {

            $('#tt').tree({
                url: '/Dictionary/LoadAllDicCategory/',
                onClick: function (node) {
                    cid = node.id;
                    $('#grid').datagrid({
                        url: "/Dictionary/LoadDictionarysByDicCategoryId/" + cid
                    });
                }
            });

            $('#grid').datagrid({
                title: '条目信息',
//                iconCls: 'icon-ok',
                nowrap: false,                      //数据是否显示在一行里
                striped: true,                      //奇偶行使用不同背景色
                url: '/Dictionary/LoadDictionarysByDicCategoryId/',       //从远程站点请求数据的 URL
                fitColumns: true,                   //适应表格的宽度并且防止水平滚动
                fit: true,
                idField: 'Id',                      //标识字段
                sortName: 'Index',                  //定义可以排序的列
                sortOrder: 'asc',                   //定义列的排序顺序，只能用 'asc' 或 'desc'
                remoteSort: true,                  //定义是否从服务器给数据排序
                singleSelect: true,                //是否允许选中一行
                pagination: true,                  //分页
                rownumbers: true,                   //行号
                pageSize: 20,                        //每页记录数
                pageList: [10,20,30],             //每页记录数列表
//                frozenColumns: [[
//	                { field: 'Id', checkbox: true}  //和列的特性一样，但是这些列将被冻结在左边
//				]],
                columns: [[
                     { field: 'Name', title: '条目名称', width: 80 }
                    , { field: 'Code', title: '条目编码', width: 80 }
                    , { field: 'InputCode1', title: '助记码1', width: 80 }
                    , { field: 'InputCode2', title: '助记码2', width: 80 }
                    , { field: 'InputCode3', title: '助记码3', width: 80 }
                    , { field: 'Index', title: '排序', width: 80 }
                    , { field: 'Description', title: '条目描述', width: 80 }
                ]]
            });
        });


        function submit() {
            $('#form').form('submit', {
                onSubmit: function () {
                    return $(this).form('validate');
                },
                success: function (msg) {
                    var msg1 = eval('(' + msg + ')');
                    if (msg1.IsSuccess) {
                        $.messager.alert('提示', "保存成功", 'info', function () {
                            if ($("#id").val() != "") {
                                $('#win').dialog('close');
                            }
                            $('#grid').datagrid('reload');
                        });
                    }
                    else {
                        $.messager.alert('提示', msg1.Message, "error");
                    }
                },
                error: function () {
                    $.messager.alert('错误', '保存失败！', "error");
                }
            });
        }

        function add() {
            if (cid == "") {
                $.messager.alert('提示', '请选择新增条目的所属分类');
            }
            else {
                $('#win').dialog({
                    title: '新增条目',
                    iconCls: 'icon-add',
                    width: 400,
                    height: 334,
                    closed: false,
                    cache: false,
                    href: '/Dictionary/Add/' + cid,
                    modal: true,
                    buttons: [{
                        text: '提交',
                        iconCls: 'icon-ok',
                        handler: submit
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#win').dialog('close');
                        }
                    }]
                });
            }
        }

        function edit() {
            var row = $('#grid').datagrid('getSelected');
            if (row) {
                $('#win').dialog({
                    title: '修改条目',
                    iconCls: 'icon-save',
                    width: 400,
                    height: 334,
                    closed: false,
                    cache: false,
                    href: '/Dictionary/Edit/' + row.Id,
                    modal: true,
                    buttons: [{
                        text: '提交',
                        iconCls: 'icon-ok',
                        handler: submit
                    }, {
                        text: '取消',
                        iconCls: 'icon-cancel',
                        handler: function () {
                            $('#win').dialog('close');
                        }
                    }]
                });
            } else {
                $.messager.alert('提示', '请选择要修改的数据');
                return;
            }
        }

        function del() {
            var rows = $('#grid').datagrid('getSelections');
            if (!rows || rows.length == 0) {
                $.messager.alert('提示', '请选择要删除的数据');
                return;
            }
            var parm;
            $.each(rows, function (i, n) {
                if (i == 0) {
                    parm = "idList=" + n.Id;
                }
                else {
                    parm += "&idList=" + n.Id;
                }
            });
            $.messager.confirm('提示', '是否删除这些数据?', function (r) {
                if (!r) {
                    return;
                }

                $.ajax({
                    type: "POST",
                    url: "/Dictionary/Delete/",
                    data: parm,
                    success: function (msg) {
                        if (msg.IsSuccess != null) {
                            if (msg.IsSuccess) {
                                $.messager.alert('提示', '删除成功！', "info", function () {
                                    $('#grid').datagrid("reload");
                                });
                            }
                            else {
                                $.messager.alert('提示', msg.Message, "error");
                            }
                        }
                        else {
                            eval(msg);
                        }
                    },
                    error: function () {
                        $.messager.alert('错误', '删除失败！', "error");
                    }
                });
            });
        }

    </script>
</head>
<body class="easyui-layout">
    <div region="west" style="width: 200px; padding: 5px; border-right: 1px solid #8DB2E3;"
        border="false">
        <ul id="tt" animate="true">
        </ul>
    </div>
    <div region="center" style="padding: 5px;" border="false">
        <table id="grid" toolbar="#toolbar">
        </table>
        <div id="toolbar" style="padding: 2px; height: auto">
            <a id="btnAdd" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-add" plain="true" onclick="add()">添加</a>
            <a id="btnUpdate" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-edit" plain="true" onclick="edit()">修改</a>
            <a id="btnDelete" class="easyui-linkbutton" href="javascript:void(0)" iconcls="icon-remove" plain="true" onclick="del()">删除</a>
        </div>
    </div>
    <div id="win" />
</body>
</html>
